//
//  ZZCShoppingController.m
//  ZZC-Finance
//
//  Created by mac on 2017/5/21.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ZZCShoppingController.h"
#import "ZZCH5WebVController.h"

@interface ZZCShoppingController ()<UIWebViewDelegate>
@property (nonatomic, strong) UITableView *tableView;

@property (weak, nonatomic) IBOutlet UIWebView *cartWebV;
@property (copy,   nonatomic) NSString *WARN;
@property (copy,   nonatomic) NSString *token;
@end

@implementation ZZCShoppingController


#pragma mark - UINavigationControllerDelegate
// 将要显示控制器
//- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
//    // 判断要显示的控制器是否是自己
//    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
//    
//    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
//}
-(void)viewWillAppear:(BOOL)animated {
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"isLogin"]) {
        ZZCLoginController *login = [ZZCLoginController new];
        [self.navigationController pushViewController:login animated:YES];
        _token = ZZCNSUserDefaults(@"token");
    } else {
        [self reloadSucceedData];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"  购物车";
    float chacha = [[NSUserDefaults standardUserDefaults] floatForKey:@"appchacha"];
    chacha += 0.02;
    [[NSUserDefaults standardUserDefaults] setFloat:chacha forKey:@"appchacha"];

    NSString *token = ZZCNSUserDefaults(@"token");
    NSURL *h5_needAsset = [NSURL URLWithString:[NSString stringWithFormat:@"%@?token=%@",ZZCURL_API_H5_CART,token]];
    NSURLRequest *request = [NSURLRequest requestWithURL:h5_needAsset];
    NSLog(@"%@",h5_needAsset);
    //设置代理
    self.cartWebV.delegate = self;
    //让内容自动适应屏幕大小
    self.cartWebV.scalesPageToFit = YES;
    [self.cartWebV loadRequest:request];
    [self.view addSubview:self.cartWebV];
    self.cartWebV.scrollView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self reloading];
    }];
}
- (void)reloading {
    NSString *token = ZZCNSUserDefaults(@"token");
    NSURL *h5_needAsset = [NSURL URLWithString:[NSString stringWithFormat:@"%@?token=%@",ZZCURL_API_H5_CART,token]];
    NSURLRequest *request = [NSURLRequest requestWithURL:h5_needAsset];
    [self.cartWebV loadRequest:request];
    [self.cartWebV.scrollView.header endRefreshing];

}
//拦截URL触发事件
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
    //    NSLog(@"------> %@",request);
    NSString *strr;
    if ([request.URL.absoluteString containsString:ZZZZURL]) {
        NSLog(@"已拦截");
        NSString *str1 = [request.URL.absoluteString  substringFromIndex:ZZZZURL.length];
        NSLog(@"%@",str1);
        _token = ZZCNSUserDefaults(@"token");
        strr = [NSString stringWithFormat:@"%@%@%@",ZZCURL_API,str1,_token];
        NSLog(@"%@",strr);
        if (![[NSUserDefaults standardUserDefaults] boolForKey:@"isLogin"]) {
            ZZCLoginController *login = [ZZCLoginController new];
            [self.navigationController pushViewController:login animated:YES];
        } else {
            ZZCH5WebVController *h5vc = [ZZCH5WebVController new];
            h5vc.h5Url = strr;
            [self.navigationController pushViewController:h5vc animated:YES];
        }
        //           [self.classifyWebV loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:strr]]];
        return NO;
    }
    //    if ([request.URL.absoluteString  containsString:@"==="]) {
    //        if ([request.URL.absoluteString  hasPrefix:@"back"]) {
    //            [self.navigationController popViewControllerAnimated:YES];
    //            NSLog(@"退出");
    //        }else{
    //            [self.classifyWebV goBack];
    //            NSLog(@"返回上一层");
    //        }
    //    }
    
    return YES;
}
//登录成功后刷新数据源
- (void)reloadSucceedData {
    NSString *token = ZZCNSUserDefaults(@"token");
    NSDictionary *dict = @{@"token":token};
    NSString *sign = [ZZCRequest getSignWithDict:dict];
    sign = [NSString md5:[sign lowercaseString]];
    NSDictionary *param = @{@"timestamp":[NSString timeStr],@"sign":sign,@"token":token};
    [[ZZCRequest sharedNetworkTools] POST:ZZCURL_API_PERSONAL parameters:param finished:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (!error) {
            ZZCLog(@"-%@",responseObject);
            if ([responseObject[@"code"] isEqual:@(301)]) {
                _WARN = responseObject[@"msg"];
                NSLog(@"账号已在别的设备登录");
                [self alertView];
            }
        }
    }];
    
}
- (void)alertView
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_WARN  message:@"请重新登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alert.alertViewStyle = UIAlertViewStyleDefault;
    [alert show];
}
//监听点击事件 代理方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *btnTitle = [alertView buttonTitleAtIndex:buttonIndex];
    if ([btnTitle isEqualToString:@"取消"]) {
        NSLog(@"你点击了取消");
    }else if ([btnTitle isEqualToString:@"确定"] ) {
        NSLog(@"你点击了确定");
        ZZCLoginController *login = [ZZCLoginController new];
        [self.navigationController pushViewController:login animated:YES];
        //        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
    }//https在iTunes中找，这里的事件是前往手机端App store下载微信
}

@end
